package org.jeecg.modules.htxincailiao.industry.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.entity.HTPlatformUser;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.industry.domain.bo.HTIndustryApplyBO;
import org.jeecg.modules.htxincailiao.industry.domain.dto.HTApplyInfoDTO;
import org.jeecg.modules.htxincailiao.industry.domain.dto.HTIndustryMeetingApplyDTO;
import org.jeecg.modules.htxincailiao.industry.entity.HTIndustryMeetingApply;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustryMeetingApplyService;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustryMeetingCheckService;
import org.jeecg.modules.htxincailiao.industry.service.IHTIndustryMeetingService;
import org.jeecg.modules.htxincailiao.order.service.IHTOrderService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.Arrays;

/**
 * @Description: ht_industry_meeting_apply
 * @Author: jeecg-boot
 * @Date: 2021-06-05
 * @Version: V1.0
 */
@Api(tags = "ht_industry_meeting_apply")
@RestController
@RequestMapping("/industry/hTIndustryMeetingApply")
@Slf4j
public class HTIndustryMeetingApplyController extends JeecgController<HTIndustryMeetingApply, IHTIndustryMeetingApplyService> {
    @Resource
    private IHTIndustryMeetingApplyService hTIndustryMeetingApplyService;

    @Resource
    private IHTIndustryMeetingService industryMeetingService;

    @Resource
    private IHTIndustryMeetingCheckService industryMeetingCheckService;

    @Resource
    private IHTOrderService orderService;

    @Resource
    private IHTAccountService accountService;

    /**
     * 分页列表查询
     *
     * @param hTIndustryMeetingApply
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-分页列表查询")
    @ApiOperation(value = "ht_industry_meeting_apply-分页列表查询", notes = "ht_industry_meeting_apply-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(HTIndustryMeetingApply hTIndustryMeetingApply,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<HTIndustryMeetingApply> queryWrapper = QueryGenerator.initQueryWrapper(hTIndustryMeetingApply, req.getParameterMap());
        Page<HTIndustryMeetingApply> page = new Page<HTIndustryMeetingApply>(pageNo, pageSize);
        IPage<HTIndustryMeetingApply> pageList = hTIndustryMeetingApplyService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTIndustryMeetingApply
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-添加")
    @ApiOperation(value = "ht_industry_meeting_apply-添加", notes = "ht_industry_meeting_apply-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTIndustryMeetingApply hTIndustryMeetingApply) {
        hTIndustryMeetingApplyService.save(hTIndustryMeetingApply);
        return Result.OK("添加成功！");
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-添加")
    @ApiOperation(value = "ht_industry_meeting_apply-添加", notes = "ht_industry_meeting_apply-添加")
    @PostMapping(value = "/userApply")
    @Transactional
    public Result<?> userApply(HttpServletRequest request, @Valid @RequestBody HTIndustryMeetingApplyDTO dto) {
        Result<JSONObject> result = new Result<>();

        if (dto.getApplyObjectType() == 0 && oConvertUtils.isEmpty(dto.getEnterpriseName())) {
            return result.error500("企业名不能为空");
        }

        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        HTPlatformUser user = accountService.queryFrontendUserByUsername(username);

        if (oConvertUtils.isNotEmpty(user)) {
            //同一个会议同一个用户只能报一次
            HTIndustryMeetingApply hasAppliedMeeting = hTIndustryMeetingApplyService.queryBy(user.getId(), dto.getMeetingId(), dto.getApplyObjectType());
            if (oConvertUtils.isNotEmpty(hasAppliedMeeting)) {
                return Result.error("不能重复报名");
            }

            System.out.printf("\n\n\n userApply %s \n\n\n", JSONObject.toJSONString(dto));
            HTIndustryApplyBO bo = hTIndustryMeetingApplyService.handleUserApply(dto, user.getId());
            result.setSuccess(bo.isSuccess());
            if (bo.isSuccess()) {
                if (oConvertUtils.isNotEmpty(bo.getPaymentItem())) {
                    JSONObject obj = new JSONObject();
                    obj.put("paymentInfo", bo.getPaymentItem());
                    result.setResult(obj);
                }
            } else {
                result.error500("报名失败，请稍后尝试");
            }
        } else {
            result.error500("请先登录");
        }
        return result;
    }

    /**
     * 添加
     *
     * @param dto
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-添加")
    @ApiOperation(value = "ht_industry_meeting_apply-添加", notes = "ht_industry_meeting_apply-添加")
    @PostMapping(value = "/getUserApplyInfo")
    @Transactional
    public Result<?> getUserApplyInfo(HttpServletRequest request, @Valid @RequestBody HTApplyInfoDTO dto) {
        Result<JSONObject> result = new Result<>();

        String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
        String username = JwtUtil.getUsername(token);
        HTPlatformUser user = accountService.queryFrontendUserByUsername(username);

        if (oConvertUtils.isNotEmpty(user)) {
            HTIndustryMeetingApply hasAppliedMeeting = hTIndustryMeetingApplyService.queryBy(user.getId(), dto.getMeetingId(), dto.getApplyObjectType());
            result.setResult(JSON.parseObject(JSONObject.toJSONString(hasAppliedMeeting)));
        } else {
            result.error500("请先登录");
        }

        return result;
    }


    /**
     * 编辑
     *
     * @param hTIndustryMeetingApply
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-编辑")
    @ApiOperation(value = "ht_industry_meeting_apply-编辑", notes = "ht_industry_meeting_apply-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTIndustryMeetingApply hTIndustryMeetingApply) {
        hTIndustryMeetingApplyService.updateById(hTIndustryMeetingApply);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-通过id删除")
    @ApiOperation(value = "ht_industry_meeting_apply-通过id删除", notes = "ht_industry_meeting_apply-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTIndustryMeetingApplyService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-批量删除")
    @ApiOperation(value = "ht_industry_meeting_apply-批量删除", notes = "ht_industry_meeting_apply-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTIndustryMeetingApplyService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_industry_meeting_apply-通过id查询")
    @ApiOperation(value = "ht_industry_meeting_apply-通过id查询", notes = "ht_industry_meeting_apply-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        HTIndustryMeetingApply hTIndustryMeetingApply = hTIndustryMeetingApplyService.getById(id);
        if (hTIndustryMeetingApply == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(hTIndustryMeetingApply);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param hTIndustryMeetingApply
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTIndustryMeetingApply hTIndustryMeetingApply) {
        return super.exportXls(request, hTIndustryMeetingApply, HTIndustryMeetingApply.class, "ht_industry_meeting_apply");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTIndustryMeetingApply.class);
    }

}
